package com.code.leetcode.before50;

import com.code.leetcode.utils.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/7/25 20:02
 */
public class The103_dfs {
    /**
     * dfs
     *
     * @param root treenode
     * @return 遍历后的层数ju'chi
     */
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> lists = new ArrayList<>();
        if (root == null) return lists;
        dfs(0, lists, root);
        return lists;
    }

    public void dfs(int level, List<List<Integer>> lists, TreeNode node) {
        if (level >= lists.size()) {
            LinkedList<Integer> list = new LinkedList<>();
            list.add(node.val);
            lists.add(list);
        } else {
            if (level % 2 == 0) {
                lists.get(level).add(node.val);
            } else {
                lists.get(level).add(0, node.val);
            }
        }
        if (node.left != null) dfs(level + 1, lists, node.left);
        if (node.right != null) dfs(level + 1, lists, node.right);
    }

}
